This tutorial shows how to use .gesture( LongPressGesture() ).
Method onEnded() accepts Closure as its last Parameter which is why Closure can be declared outside Parameters List.
In the below example circle changes color when you Long Press on it.
TapGesture().onEnded()
struct ContentView: View {
@State var color = true
var body: some View {
Circle()
.fill(color ? Color.green : Color.red)
.frame(width: 50, height: 50)
.gesture( LongPressGesture(minimumDuration: 1, maximumDistance: 1)
.onEnded { value in //Always true
self.color.toggle()
}
)
}
}
Initial color Tap to change color